//#pragma once
#include"graphics.h"

//**************************************判断胜利

extern int count;
extern WINDOW* g_win;
extern GRAPHICS_COLOR g_fore_color;
extern GRAPHICS_COLOR g_back_color;
extern int g_style;
bool judgeVictor(POINT* head)
{
	bool flag;
	POINT* p = head->next;
	POINT* tmp=p;
	POINT victor[5];
	
	int i;
	int n;
	for(i=0; i<5; i++)		//判断横轴胜利
	{
		n=0;			//初始化
		tmp = p;		//初始化
		while(1)
		{

			if(tmp == NULL)
				break;
			if(tmp->x == p->x && (tmp->y) >= (p->y)+(i-4)*WIDTH && (tmp->y) <= (p->y)+i*WIDTH )
			{	
				victor[n].x = tmp->x;
				victor[n].y = tmp->y;
				n++;
			}
			
			tmp=tmp->next;
			if(tmp == NULL)
				break;
			tmp=tmp->next;	//两个子一回合
	
		}	

		if(n == 5)
		{
			if(count%2 != 0)		
				flag = 1;	//白子胜
			else 
				flag = 0;	//黑子胜
			goto last;
		}		
	}
	
	for(i=0; i<5; i++)		//判断竖轴胜利
	{
		n=0;			//初始化
		tmp = p;		//初始化
		while(1)
		{

			if(tmp == NULL)
				break;
			if(tmp->y == p->y && (tmp->x) >= (p->x)+(i-4)*LENGTH && (tmp->x) <= (p->x)+i*LENGTH )
			{	
				victor[n].x = tmp->x;
				victor[n].y = tmp->y;
				n++;
			}
			
			tmp=tmp->next;
			if(tmp == NULL)
				break;
			tmp=tmp->next;	//两个子一回合
	
		}	

		if(n == 5)
		{
			if(count%2 != 0)		
				flag = 1;	//白子胜
			else 
				flag = 0;	//黑子胜
			goto last;
		}		
	}	
	
	for(i=0; i<5; i++)		//判断向左斜轴胜利
	{
		n=0;			//初始化
		tmp = p;		//初始化
		while(1)
		{

			if(tmp == NULL)
				break;
			if( (tmp->x == p->x+(i-4)*LENGTH && tmp->y == p->y+(i-4)*WIDTH) ||  (tmp->x == p->x+(i-3)*LENGTH && tmp->y == p->y+(i-3)*WIDTH) 
 || (tmp->x == p->x+(i-2)*LENGTH && tmp->y == p->y+(i-2)*WIDTH) || (tmp->x == p->x+(i-1)*LENGTH && tmp->y == p->y+(i-1)*WIDTH) || (tmp->x == p->x+(i)*LENGTH && tmp->y == p->y+(i)*WIDTH))
			{	
				victor[n].x = tmp->x;
				victor[n].y = tmp->y;
				n++;
			}
			
			tmp=tmp->next;
			if(tmp == NULL)
				break;
			tmp=tmp->next;	//两个子一回合
	
		}	

		if(n == 5)
		{
			if(count%2 != 0)		
				flag = 1;	//白子胜
			else 
				flag = 0;	//黑子胜
			goto last;
		}		
	}
	
		
	for(i=0; i<5; i++)		//判断向右斜轴胜利
	{
		n=0;			//初始化
		tmp = p;		//初始化
		while(1)
		{

			if(tmp == NULL)
				break;
			if( (tmp->x == p->x+(i-4)*LENGTH && tmp->y == p->y+(4-i)*WIDTH) ||  (tmp->x == p->x+(i-3)*LENGTH && tmp->y == p->y+(3-i)*WIDTH) 
 || (tmp->x == p->x+(i-2)*LENGTH && tmp->y == p->y+(2-i)*WIDTH) || (tmp->x == p->x+(i-1)*LENGTH && tmp->y == p->y+(1-i)*WIDTH) || (tmp->x == p->x+(i)*LENGTH && tmp->y == p->y+(-i)*WIDTH))
			{	
				victor[n].x = tmp->x;
				victor[n].y = tmp->y;
				n++;
			}
			
			tmp=tmp->next;
			if(tmp == NULL)
				break;
			tmp=tmp->next;	//两个子一回合
	
		}	

		if(n == 5)
		{
			if(count%2 != 0)		
				flag = 1;	//白子胜
			else 
				flag = 0;	//黑子胜
			goto last;
		}		
	}
	
	return false;
last:
	g_back_color = GCGreen;				//设置背景色
	if(flag == 1)
	{
		g_fore_color=GCBlue;				//设置前景色
		wattrset(g_win,CURRENT_ATTRIBUTE);		//更新前景色和背景色
		
		for(i=0; i<5; i++)
			mvaddstr(victor[i].x, victor[i].y, "  ");
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化
		sleep(1.5);
		
		g_fore_color=GCWhite;			//设置前景色
		wattrset(g_win,CURRENT_ATTRIBUTE);		//更新前景色和背景色
		for(i=0; i<5; i++)
			mvaddstr(victor[i].x, victor[i].y, CHESS);
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化	
		
		mvaddstr(4, 10, "白棋胜利!!");	
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化
		sleep(2);
	}	
	else
	{

		g_fore_color=GCGreen;			//设置前景色
		wattrset(g_win,CURRENT_ATTRIBUTE);		//更新前景色和背景色
		
		for(i=0; i<5; i++)
			mvaddstr(victor[i].x, victor[i].y, "  ");
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化
		sleep(1.5);
		
		g_fore_color=GCBlack;			//设置前景色
		wattrset(g_win,CURRENT_ATTRIBUTE);		//更新前景色和背景色
		for(i=0; i<5; i++)
			mvaddstr(victor[i].x, victor[i].y, CHESS);
		
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化	
		sleep(1);
		mvaddstr(4, 10, "黑棋胜利胜利!!");
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化
		sleep(2);	
	}
	return true;
	
}
